package com.globant.dashboard.webapp.controllers;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.globant.dashboard.model.Language;
import com.globant.dashboard.model.User;
import com.globant.dashboard.service.UserService;

@org.springframework.stereotype.Controller
public class UserController {

	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "user/main.do", method = RequestMethod.GET)
	public String userMainGet(Model model) {

		Object principal = SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();
		UserDetails userDetails = null;
		// if (principal instanceof UserDetails) {
			userDetails = (UserDetails) principal;
		// }
		String email = userDetails.getUsername();
		User user = userService.getUserByEmail(email);

		final String currentUser = user.getName();
		//String message = "";

		model.addAttribute("username", currentUser);
		//model.addAttribute("message", message);

		return "userMain";
	}

	@RequestMapping(value = "user/main.do", method = RequestMethod.POST)
	public String userMainPost(Model model) {

		Object principal = SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();
		UserDetails userDetails = null;
		// if (principal instanceof UserDetails) {
			userDetails = (UserDetails) principal;
		// }
		String email = userDetails.getUsername();
		User user = userService.getUserByEmail(email);

		final String currentUser = user.getName();

		model.addAttribute("username", currentUser);

		return "userMain";
	}

	@RequestMapping(value = "user/editUser.do", method = RequestMethod.GET)
	public String updateUser(Model model) {

		Object principal = SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();
		UserDetails userDetails = null;
		// if (principal instanceof UserDetails) {
			userDetails = (UserDetails) principal;
		// }
		String email = userDetails.getUsername();
		User user = userService.getUserByEmail(email);

		// final String currentUser = user.getName();
		model.addAttribute(user);
		model.addAttribute("availableLanguages", Language.getAvailableLanguages());

		return "updateMyProfile";
	}

	@RequestMapping(value = "user/update.do", method = RequestMethod.POST)
	public String updateUserPost(Model model, HttpServletRequest request, User user) {
		try {
			userService.updateUser(user);

			if(!user.getEmail().equals(request.getParameter("emailModified"))) {
				SecurityContextHolder.getContext().getAuthentication().setAuthenticated(false);
			}
			//model.addAttribute("successMsg", "The profile has been upadated.");
		} catch (Exception e) {
			model.addAttribute("user", user);
			model.addAttribute("availableLanguages", Language.getAvailableLanguages());
			model.addAttribute("errorMsg", e.getMessage());
			return "updateMyProfile";
		}

		return "redirect:/user/main.do";
	}	  
}